import java.net.InetAddress;
import java.net.UnknownHostException;

// 获取IP、主机名
public class Test1 {
    public static void main(String[] args) throws UnknownHostException {
        // 查询本地ip地址
        InetAddress address1 = InetAddress.getByName("192.168.52.49");
        InetAddress address2 = InetAddress.getByName("127.0.0.1");
        InetAddress address3 = InetAddress.getByName("localhost");
        // 查询网站ip地址
        InetAddress address4 = InetAddress.getByName("www.baidu.com");
        System.out.println(address1);
        System.out.println(address2);
        System.out.println(address3);
        System.out.println(address4);
        // 常用方法:获取自己电脑的名字
        System.out.println("==============");
        System.out.println(address2.getAddress());
        System.out.println(address2.getHostAddress());   //ip
        System.out.println(address2.getHostName());   //获取的是与IP地址关联的主机名
        System.out.println(InetAddress.getLocalHost().getHostName());  // 获取主机名
        /*
            /192.168.52.49
            /127.0.0.1
            localhost/127.0.0.1
            www.baidu.com/180.101.50.188
            ==============
            [B@1b6d3586
            127.0.0.1
            activate.navicat.com
            TNZB-227221DA
         */
    }
}
